package com.dz51.core.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.dz51.core.basedao.dao.impl.HibernateAbstractSupportDao;
import com.dz51.core.basedao.query.IConditionBuilder;
import com.dz51.core.basedao.query.impl.DefaultQueryConditionBuilder;
import com.dz51.core.page.commond.BasePageCommond;
import com.dz51.core.service.IBaseService;

/**
 * @Title: AbstractBaseServiceImpl.java
 * @Package com.dz51.core.service.impl
 * @Description: <p>基础service层实现
 * <p>------------- 提供数据库增、删、查、改 ----------
 * @author Zhaoqt  
 * @date 2014-5-11 上午6:02:24
 * @version V1.0
 */
public abstract class AbstractBaseServiceImpl<E> implements IBaseService<E> {

	public abstract HibernateAbstractSupportDao<E> getHibernateAbstractSupportDao();
	
	/**
	 * @Title: loadEntity
	 * @Description:  session.load(..)
	 * @param id
	 * @throws Exception    
	 * @return E
	 */
	@Override
	public Object loadEntity(Serializable id) throws Exception {
		return getHibernateAbstractSupportDao().loadEntity(id);
	}

	/**
	 * @Title: getEntity
	 * @Description: session.get(..) 
	 * @param id
	 * @throws Exception    
	 * @return E
	 */
	@Override
	public Object getEntity(Serializable id) throws Exception {
		return getHibernateAbstractSupportDao().getEntity(id);
	}

	/**
	 * @Title: addEntity
	 * @Description: 实体储存 
	 * @param entity
	 * @throws Exception    
	 * @return void
	 */
	@Override
	public void addEntity(E entity) throws Exception {
		getHibernateAbstractSupportDao().addEntity(entity);
	}

	/**
	 * @Title: batchAddEntities
	 * @Description: 实体批量储存
	 * @param collection
	 * @throws Exception    
	 * @return void
	 */
	@Override
	public void batchAddEntities(Collection<E> collection) throws Exception {
		getHibernateAbstractSupportDao().batchAddEntities(collection);
	}

	/**
	 * @Title: updateEntityByPK
	 * @Description: 主键更新实体 该方法不支持动态更新慎用
	 * @param entity
	 * @throws Exception    
	 * @return void
	 */
	@Override
	public void updateEntityByPK(E entity) throws Exception {
		getHibernateAbstractSupportDao().updateEntityByPK(entity);
	}
	
	/**
	 * @Title: deleteEntityByPK
	 * @Description: 主键删除实体 
	 * @param entity
	 * @throws Exception    
	 * @return void
	 */
	@Override
	public void deleteEntityByPK(E entity) throws Exception {
		getHibernateAbstractSupportDao().deleteEntityByPK(entity);
	}

	/**
	 * @Title: batchDeleteEntities
	 * @Description: 实体批量删除 
	 * @param collection
	 * @throws Exception    
	 * @return void
	 */
	@Override
	public void batchDeleteEntities(Collection<E> collection) throws Exception {
		getHibernateAbstractSupportDao().batchDeleteEntities(collection);
	}

	/**
	 * @Title: loadEntity
	 * @Description:  session.load(..)
	 * @param className
	 * @param id
	 * @throws Exception    
	 * @return E
	 */
	@Override
	public E loadEntity(Class<E> className, Serializable id) throws Exception {
		return getHibernateAbstractSupportDao().loadEntity(className, id);
	}

	/**
	 * @Title: getEntity
	 * @Description: session.get(..) 
	 * @param className
	 * @param id
	 * @throws Exception    
	 * @return E
	 */
	@Override
	public E getEntity(Class<E> className, Serializable id) throws Exception {
		return getHibernateAbstractSupportDao().getEntity(className, id);
	}
	
	/**
	 * 基础列表查询
	 * @Title: findList
	 * @param basePageCommond
	 * @return
	 */
	@Override
	public List<?> findList(BasePageCommond basePageCommond) throws Exception {
		IConditionBuilder conditionBuilder = new DefaultQueryConditionBuilder();
		// 支持Criteria查询
		conditionBuilder.isCriteria(true);
		conditionBuilder.setKeyword(basePageCommond.getKeyword(), basePageCommond.getProperty());
		conditionBuilder.addPager(basePageCommond.getPager());
		conditionBuilder.addSort(basePageCommond.getOrderType(), basePageCommond.getOrderBy());
		basePageCommond = getHibernateAbstractSupportDao().find(conditionBuilder);
		
		return (List<?> )basePageCommond.getResultCollection();
	}
	
	
}
